<template>
  <div style="margin-bottom: 15px">
    <div class="left-title">加工产品信息</div>
    <div class="display-flex justify-content-flex-justify left-title">
      <div>材料信息</div>
      <div>
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleMaterialAdd"
          >新增</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleMaterialDelete"
          >删除</el-button
        >
      </div>
    </div>
    <el-table
      :cell-style="{ padding: this.tablePadding }"
      border
      :data="setList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="材料名称" align="center" prop="materialName" />
    </el-table>
    <el-form
      :model="query"
      ref="queryForm"
      label-width="70px"
      style="margin-top: 15px"
    >
      <el-form-item label="产品名称" prop="deptId">
        <el-input
          v-model="query.productName"
          placeholder="请输入故障描述"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
    </el-form>
    <el-dialog :close-on-click-modal="false"
      title="选择材料"
      :visible.sync="open"
      width="800px"
      append-to-body
    >
      <el-row :gutter="20">
        <el-col :span="5">
          <left-tree
            @getParentId="getParentIdFunc"
            @cateChange="cateChange"
            :isShowBtn="false"
          />
        </el-col>
        <el-col :span="19">
          <el-table
            :cell-style="{ padding: this.tablePadding }"
            v-loading="loading"
            :data="list"
            highlight-current-row
            height="300"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="材料ID" align="center" prop="materialId" />
            <el-table-column
              label="材料名称"
              align="center"
              prop="materialName"
            />
            <el-table-column
              label="材料类型"
              align="center"
              prop="materialCategoryName"
            />
            <el-table-column
              label="供应商"
              align="center"
              prop="supplierName"
            />
            <el-table-column label="计量单位" align="center" prop="unitName" />
            <el-table-column label="单价" align="center" prop="materialPrice" />
            <el-table-column label="备注" align="center" prop="remark" />
            <el-table-column label="建立人" align="center" prop="createBy" />
            <el-table-column
              label="建立时间"
              align="center"
              prop="createTime"
              width="155"
            />
          </el-table>

          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMaterial, listCategory } from "@/api/base/material";
import leftTree from "../../../base/materialInformation/components/Left.vue";
export default {
  components: { leftTree },
  data() {
    return {
      list: [],
      categoryList: [],
      defaultProps: {
        children: "children",
        label: "paramCategoryName",
      },
      setList: [],
      query: { productName: "" },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialName: null,
        materialCategoryId: null,
      },
      loading: false,
      total: 0,
      open: false,
      selectionItems: [],
      multiple: true,
    };
  },
  methods: {
    handleMaterialDelete() {},
    getQuery() {
      let ids = this.setList.map((item) => {
        return item.materialId;
      });
      return {
        productName: this.query.productName,
        MaterialIds: ids,
      };
    },
    handleMaterialAdd() {
      this.open = true;
      this.selectionItems = [];
    },
    cancel() {
      this.open = false;
    },
    submitForm() {
      if (this.setList.length == 0) {
        this.setList = JSON.parse(JSON.stringify(this.selectionItems));
        this.cancel();
        return;
      }
      let arr = this.selectionItems.filter(
        (item) =>
          !this.setList.some((ele) => ele.materialId === item.materialId)
      );
      arr.forEach((item) => {
        this.setList.push(item);
      });
      this.cancel();
    },
    handleSelectionChange(selection) {
      this.selectionItems = selection;
      this.multiple = !selection.length;
    },
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.materialCategoryId,
        label: node.materialCategoryName,
        children: node.children,
      };
    },
    cateChange() {
      this.getCategoryList();
    },
    getCategoryList() {
      listCategory().then((response) => {
        this.categoryList = response.data;
      });
    },
    getParentIdFunc(len, id) {
      if (len > 0) {
        this.queryParams.materialCategoryId = id;
        this.getList();
      } else {
        this.loading = false;
      }
    },
    /** 查询参数定义列表 */
    getList() {
      this.loading = true;
      listMaterial(this.queryParams).then((response) => {
        this.list = response.rows;

        this.total = response.total;
        this.loading = false;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.left-title {
  padding-bottom: 20px;
  font-size: 14px;
  color: #606266;
  font-weight: bold;
}
</style>>